package com.zbkj.front.controller;

import com.zbkj.dal.common.exception.CrmebException;
import com.zbkj.dal.pojo.request.PageParamRequest;
import com.zbkj.dal.pojo.request.ba.CreateBookingRequest;
import com.zbkj.dal.pojo.response.ba.BookingResponse;
import com.zbkj.dal.base.result.CommonResult;
import com.zbkj.service.service.BookingService;
import com.zbkj.service.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("api/front/she/booking")
@Api(tags = "卡座预约")
public class SheBookingController {
    @Autowired
    private BookingService bookingService;
    @Autowired
    private UserService userService;

    /**
     * 我的预约
     * @param status
     * @param pageParamRequest
     * @return
     */
    @ApiOperation(value = "我的卡座预约列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<List<BookingResponse>> listMyBooking(Integer status, PageParamRequest pageParamRequest) {
        return CommonResult.success(bookingService.listMyBooking(status, pageParamRequest));
    }

    /**
     * 删除我的预约
     * @param id
     */
    @ApiOperation(value = "删除我的卡座预约",notes = "只能删已提交的")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    public CommonResult<Boolean> deleteMyBooking(@PathVariable("id")Integer id) {
        return CommonResult.success(bookingService.deleteMyBooking(id));
    }

    /**
     * 创建
     * @param createBookingRequest
     * @return
     */
    @ApiOperation(value = "提交卡座预约信息")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public CommonResult<Boolean> create(@Validated @RequestBody CreateBookingRequest createBookingRequest) {
        return CommonResult.success(bookingService.create(createBookingRequest));
    }

    /**
     * 预约详情（注意，本方法供前后台使用，如果判“我的预约“，请自行验证）
     * @param id
     * @return
     */
    @ApiOperation(value = "卡座预约详情")
    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    public CommonResult<BookingResponse> detail(@PathVariable("id") Integer id) {
        Integer userId = userService.getUserIdException();
        BookingResponse booking = bookingService.detail(id);
        if (!ObjectUtils.nullSafeEquals(booking.getUid(), userId)){
            throw new CrmebException("您不能查看该信息");
        }
        return CommonResult.success(booking);
    }
}
